from pynput import keyboard,mouse
import threading,queue
def on_click(x,y, button, pressed): 
    return
left_ctr_pressed = False
right_ctr_pressed = False
shift_pressed =False
alt_pressed = False

def x_effect():
    if left_ctr_pressed and alt_pressed:
        return True
    return False
def y_effect():
    if right_ctr_pressed and alt_pressed:
        return True
    return False

start_pos = (0,0)
q = queue.Queue(1000)

def get_last():
    a = q.get()
    while True:
        try:
            a = q.get(False,0)
        except:
            return a
            
def do_move():
    while True:
        x,y = get_last()
        # print('get',x,y)
        mouse.Controller().move(x,y)
t = threading.Thread(target=do_move)
t.daemon = True
t.start()

def on_move(x,y):
    # print(x,y)
    try:
        if x_effect() and x!=start_pos[0]:
            # print('move',start_pos[0]-x,0)
            q.put((start_pos[0]-x,0),False)
            return
        
        if y_effect() and y!=start_pos[1]:
            q.put((0,start_pos[1]-y),False)
            # print('move',0,start_pos[1]-y)
    except:
        pass

import time
def on_release(key):    
    global left_ctr_pressed,right_ctr_pressed
    global alt_pressed,shift_pressed
    x = str(key)
    # print('r',x)
    if 'shift' in x:
        shift_pressed = False
    if 'alt' in x:
        alt_pressed = False
             
    if x=='Key.ctrl_l':
        left_ctr_pressed = False
    if x=='Key.ctrl_r':
        right_ctr_pressed =False
def on_press(key):    
    global left_ctr_pressed,right_ctr_pressed,start_pos
    global alt_pressed,shift_pressed
    x = str(key)
    # print('p',x)
    if 'shift' in x:
        shift_pressed = True
    if 'alt' in x:
        alt_pressed = True

    if x=='Key.ctrl_l' and not left_ctr_pressed:
        left_ctr_pressed = True
        start_pos = mouse.Controller().position
        print('start_pos',start_pos)
    if x=='Key.ctrl_r' and not right_ctr_pressed:
        right_ctr_pressed =True
        start_pos = mouse.Controller().position
        print('start_pos',start_pos)
listener = keyboard.Listener(
    on_press=on_press,
    on_release=on_release
)   
listener.start()
def testa():    
    mouse_listener = mouse.Listener(on_click=on_click,on_move=on_move)
    mouse_listener.start()
    mouse_listener.join()
testa()